package com.jfinalnat.httpclient.jfinal.aop;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jfinal.aop.Interceptor;
import com.jfinal.aop.Invocation;
import com.jfinal.core.Controller;
import com.jfinalnat.httpclient.annotation.Proxy;
import com.jfinalnat.httpclient.sdk.DefaultHttpProxy;

public class ProxyAnnotationInterceptor implements Interceptor {

	public void intercept(Invocation inv) {
		
		Proxy ma = inv.getMethod().getAnnotation(Proxy.class);
		
		if (ma != null) {
			
			String uri = ma.uri();
			Controller c = inv.getController();
			HttpServletRequest request = c.getRequest();
            HttpServletResponse response = c.getResponse();
            
            if (uri.equals("")) {
            	new DefaultHttpProxy().execute(request, response);
            } else {            	
            	new DefaultHttpProxy().execute(request, response, uri);
            }
			c.renderNull();
			
			return;
			
		} else {
			
			inv.invoke();
			
		}
	}

}
